package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.form.Action;
import mvc.form.ActionForm;
import mvc.until.FullBean;

public class myServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public myServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//System.out.println("sign="+request.getParameter("sign"));
		ActionForm loginForm =(ActionForm) FullBean.full(request);
		Action action = null;
		
		 Map<String,String> actionMap= ActionMapping.getMapping();
		 try{
			 String className = actionMap.get(request.getParameter("sign"));
			 Class clazz= Class.forName(className);
			 action = (Action)clazz.newInstance();
			 
			 
			 
		 }catch(Exception e) {
			 
		 }
//		if(request.getParameter("sign").equals("mvc.form.LoginForm")){
//			action=new LoginAction();
//		}
//		if (request.getParameter("sign").equals("mvc.form.Zhuce")) {
//			action=new ZhuceAction();
//		}
		String message = action.execute(loginForm);
		PrintWriter print = response.getWriter();
		print.print(message);
		print.flush();
		print.close();
		//System.out.print(loginForm);
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the GET method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
